#!/bin/bash
# 部署脚本

set -e

echo "=========================================="
echo "开始部署南昌旅游路线规划系统..."
echo "=========================================="

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "错误: 未找到Python3，请先安装Python3"
    exit 1
fi

# 检查Node.js环境
if ! command -v node &> /dev/null; then
    echo "警告: 未找到Node.js，将跳过前端部署"
    SKIP_FRONTEND=true
fi

# 后端部署
echo ""
echo "开始部署后端..."
cd backend

# 创建虚拟环境（如果不存在）
if [ ! -d "venv" ]; then
    echo "创建Python虚拟环境..."
    python3 -m venv venv
fi

# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate  # Windows: venv\Scripts\activate

# 安装依赖
echo "安装Python依赖..."
pip install -r requirements.txt

# 运行数据库迁移
echo "运行数据库迁移..."
python manage.py makemigrations
python manage.py migrate

# 收集静态文件
echo "收集静态文件..."
python manage.py collectstatic --noinput

echo "后端部署完成！"
cd ..

# 前端部署
if [ "$SKIP_FRONTEND" != "true" ]; then
    echo ""
    echo "开始部署前端..."
    cd frontend

    # 安装依赖
    if [ ! -d "node_modules" ]; then
        echo "安装Node.js依赖..."
        npm install
    fi

    # 构建生产版本
    echo "构建前端生产版本..."
    npm run build

    echo "前端部署完成！"
    cd ..
fi

echo ""
echo "=========================================="
echo "部署完成！"
echo "=========================================="
echo ""
echo "启动后端服务:"
echo "  cd backend"
echo "  source venv/bin/activate"
echo "  python manage.py runserver"
echo ""
echo "启动前端服务:"
echo "  cd frontend"
echo "  npm run serve"
echo ""

